package com.lemon.smartsafetynew.ui.adapter.record;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.entity.common.DateTypeEnum;
import com.lemon.smartsafetynew.entity.record.CertificateVo;
import com.lemon.smartsafetynew.util.common.CommonUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by JiangAn on 2018/5/25.
 */

public class CertificateInfoAdapter extends LemonRcvAdapter<CertificateVo> {
    private RecyclerTClickListener<CertificateVo> mListener;

    public CertificateInfoAdapter(@Nullable List<CertificateVo> data, RecyclerTClickListener<CertificateVo> mListener) {
        super(data);
        this.mListener = mListener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new CertificateInfoAdapter.CertificateItem();
    }

    public class CertificateItem implements AdapterItem<CertificateVo> {

        @Bind(R.id.tv_space_name)
        TextView tvSpaceName;
        @Bind(R.id.tv_birth_date)
        TextView tvBirthDate;
        @Bind(R.id.tv_certificate_type)
        TextView tvCertificateType;
        @Bind(R.id.tv_send_certificate)
        TextView tvSendCertificate;
        @Bind(R.id.tv_validity_date)
        TextView tvValidityDate;
        @Bind(R.id.tv_date)
        TextView tvDate;
        @Bind(R.id.item_layout)
        LinearLayout itemLayout;
        @Bind(R.id.frameLayout)
        FrameLayout frameLayout;
        private CertificateVo data;
        private int pos;

        @Override
        public int getLayoutResId() {
            return R.layout.layout_certificate_item;
        }

        @Override
        public void bindViews(View root) {
            ButterKnife.bind(this, root);
        }

        @Override
        public void setViews() {
            itemLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(data, pos);
                }
            });
        }

        @Override
        public void handleData(CertificateVo certificateVo, int position) {
            data = certificateVo;
            pos = position;
            tvSpaceName.setText(CommonUtil.avoidNullString(certificateVo.getName()));
            tvBirthDate.setText(CommonUtil.secondToTimeStr(certificateVo.getBirth_date(), DateTypeEnum.DAY));
            tvCertificateType.setText(CommonUtil.avoidNullString(certificateVo.getLicence_type()));
            tvSendCertificate.setText(CommonUtil.avoidNullString(certificateVo.getIssue_department()));
            tvValidityDate.setText(CommonUtil.secondToTimeStr(certificateVo.getEnd_date(), DateTypeEnum.DAY));
            if(certificateVo.getDate_flag() != null){
                int status = certificateVo.getDate_flag(); //0 正常  1快过期 2已过期
                if(status == 0){
                    frameLayout.setBackgroundResource(R.mipmap.onecom_stamp1);
                    tvDate.setTextColor(Color.parseColor("#458bd0"));
                }else if(status == 1){
                    frameLayout.setBackgroundResource(R.mipmap.onecom_stamp2);
                    tvDate.setTextColor(Color.parseColor("#FF5B00"));
                }else if(status == 2){
                    frameLayout.setBackgroundResource(R.mipmap.onecom_stamp3);
                    tvDate.setTextColor(Color.parseColor("#FF0202"));
                }
            }
            tvDate.setText("下次审核日期\n" + CommonUtil.secondToTimeStr(certificateVo.getNext_date(), DateTypeEnum.DAY));
        }
    }
}

